import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale.Category;

import net.htmlparser.jericho.Element;
import net.htmlparser.jericho.Source;

import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;



public class RssParseThread extends Thread{
	String m_url;
	int m_categoryId;
	
	public RssParseThread(String _url, int category){
		this.m_url = _url;
		this.m_categoryId = category;
	}
	
	public void run()
	{
		while(true)
		{
			System.out.println("Run cartegori : " + this.m_categoryId);
			HttpClient parseHttpClient = new DefaultHttpClient();
			HttpGet parseHttpGet = new HttpGet(this.m_url);
			try {
				HttpEntity parseResEntity = parseHttpClient.execute(parseHttpGet).getEntity();
				
				String rssDocContents = EntityUtils.toString(parseResEntity);
				
				//���ڵ� ��ȯ
				rssDocContents = new String(rssDocContents.getBytes("ISO-8859-1"), "UTF-8");
				
				Source parserRssDoc = new Source(rssDocContents);
				
				List<Element> rssItemList = parserRssDoc.getAllElements("item");
				
				List<Article> articleList = new ArrayList<Article>();
				for(Element rssItem : rssItemList)
				{
					String title = this.cdata(rssItem.getFirstElement("title").getContent().toString());
					String url = this.cdata(rssItem.getAllElements().get(4).toString());
					String pubDate = rssItem.getFirstElement("pubdate").getContent().toString();
					String thumb = "";			
					if(rssItem.getAllElements("media:content").size() > 0)
						thumb = rssItem.getFirstElement("media:content").getAttributeValue("url");
					
					Article article = new Article(pubDate, title, url, thumb, m_categoryId);
					if(article.getArticleContents() != null)
						articleList.add(article);
				}
				
				System.out.println("END cartegori : " + this.m_categoryId);

				System.out.println("PUSH to db cartegori : " + this.m_categoryId);

				DBManager yoman_db_push = DBManager.getInstance();
				for(Article art : articleList)
					yoman_db_push.insertArticle(art);
				articleList.clear();
				System.out.println("PUSH END cartegori : " + this.m_categoryId);				
				//System.out.println("End!");
			} catch (ClientProtocolException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			try {
				this.sleep(1000 * 60);
				
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
	
	public String cdata(String _data)
	{
		int startIndex, endIndex;
		startIndex = _data.indexOf("<![CDATA[");
		endIndex = _data.lastIndexOf("]>");
		
		if(startIndex == -1 || endIndex == -1)
			return "";
		
		startIndex = "<![CDATA[".length();
		endIndex -= 1;
		
		return _data.substring(startIndex, endIndex).toString();
	}
}
